import java.util.Iterator;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.TableEditor;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableItem;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;

public class Validator {

	public static void validateSDLByNode(Table table, TableItem item,
			Node node, List menuItemListByNode) {
		if (((MenuItem) menuItemListByNode.get(0)).getSelection()) {
			checkVersionFormat(table, item, node);
		}
		if (((MenuItem) menuItemListByNode.get(1)).getSelection()) {
			checkReadOnlyProperty(table, item, node);
		}
		if (((MenuItem) menuItemListByNode.get(2)).getSelection()) {
			checkHiddenProperty(table, item, node);
		}
		if (((MenuItem) menuItemListByNode.get(3)).getSelection()) {
			checkMaxLength(table, item, node);
		}
		if (((MenuItem) menuItemListByNode.get(4)).getSelection()) {
			checkPosition(table, item, node);
		}
		if (((MenuItem) menuItemListByNode.get(5)).getSelection()) {
			checkId(table, item, node);
		}
	}

	public static void validateSDL(Table table, List listItem, List listNode,
			List menuItemList) {
		if (((MenuItem) menuItemList.get(0)).getSelection()) {
			checkRelatePosition(table, listItem, listNode);
		}
		if (((MenuItem) menuItemList.get(1)).getSelection()) {
			checkRelateId(table, listItem, listNode);
		}
	}

	public static void validateSDLByInputFile(Table table, TableItem item,
			Node node, List itemRules, List menuItemListByNode) {
		if (((MenuItem) menuItemListByNode.get(6)).getSelection()
				&& (node.getNodeName().equals(ComponentClass.label) || node
						.getNodeName().equals(ComponentClass.text))) {
			checkItemRulesList(table, item, node, itemRules);
		}
	}

	private static void checkVersionFormat(Table table, TableItem item,
			Node node) {
		if (!node.getNodeName().equals(ComponentClass.window)) {
			return;
		}
		Node node2 = node.getAttributes().getNamedItem(PropertyClass.version);
		if (node2 == null) {
			return;
		}
		String version = XMLUtil.getData(node2.getNodeValue(), "");
		// Pattern p = Pattern.compile(".+@.+\\.[a-z]+");
		Pattern versionPattern = Pattern.compile("[0-9]+");
		Matcher m = versionPattern.matcher(version);
		boolean matchFound = m.matches();
		if (!matchFound || version.length() != 4) {
			for (int i = 0; i < item.getParent().getColumnCount(); i++) {
				if (item.getParent().getColumn(i).getText().equals(
						PropertyClass.version)) {
					item.setBackground(i, Const.RED);
				}
			}
			TableItem temp = new TableItem(table, SWT.NONE);
			temp.setText(0, String.valueOf(table.getItemCount()));
			temp.setText(1, " Wrong version format ");
			temp.setText(2, version);
			temp.setText(3, node.getAttributes().getNamedItem(PropertyClass.id)
					.getNodeValue());
			temp.setBackground(Const.RED);
			temp.setData(Const.TABLEITEM, item);
		} else {
			item.setBackground(Const.DEFAULT_COLOR);
		}
	}

	private static void checkReadOnlyProperty(Table table, TableItem item,
			Node node) {
		if (!node.getNodeName().equals(ComponentClass.text)) {
			return;
		}
		NamedNodeMap map = node.getAttributes();
		if (map.getNamedItem(PropertyClass.readonly) == null) {
			return;
		}
		String[] fieldToCheck = new String[] { PropertyClass.capitaltype,
				PropertyClass.returnitem, PropertyClass.senditem,
				PropertyClass.kind, PropertyClass.maxlen, PropertyClass.minlen,
				PropertyClass.maxnum, PropertyClass.minnum,
				PropertyClass.necessary, PropertyClass.url,
				PropertyClass.enabled };
		for (int j = 0; j < fieldToCheck.length; j++) {
			if (map.getNamedItem(fieldToCheck[j]) != null) {
				for (int i = 0; i < item.getParent().getColumnCount(); i++) {
					String property = item.getParent().getColumn(i).getText();
					if (property.equals(fieldToCheck[j])) {
						item.setBackground(i, Const.RED);
					}
				}
				TableItem temp = new TableItem(table, SWT.NONE);
				temp.setText(0, String.valueOf(table.getItemCount()));
				temp.setText(1, " Readonly Field ");
				temp.setText(2, " No need \"" + fieldToCheck[j] + "\"");
				temp.setText(3, map.getNamedItem(PropertyClass.id)
						.getNodeValue());
				temp.setBackground(Const.RED);
				temp.setData(Const.TABLEITEM, item);
			}
		}
	}

	private static void checkHiddenProperty(Table table, TableItem item,
			Node node) {
		NamedNodeMap map = node.getAttributes();
		if (map.getNamedItem(PropertyClass.id).getNodeValue().indexOf(
				Const.HID_) < 0) {
			return;
		}
		if (map.getNamedItem(PropertyClass.visible) == null) {
			return;
		}
		String[] fieldToCheck = new String[] { PropertyClass.capitaltype,
				PropertyClass.readonly, PropertyClass.returnitem,
				PropertyClass.senditem, PropertyClass.kind,
				PropertyClass.maxlen, PropertyClass.minlen,
				PropertyClass.maxnum, PropertyClass.minnum,
				PropertyClass.necessary, PropertyClass.type,
				PropertyClass.alignment, PropertyClass.enabled,
				PropertyClass.backcolor };
		for (int j = 0; j < fieldToCheck.length; j++) {
			if (map.getNamedItem(fieldToCheck[j]) != null) {
				for (int i = 0; i < item.getParent().getColumnCount(); i++) {
					String property = item.getParent().getColumn(i).getText();
					if (property.equals(fieldToCheck[j])) {
						item.setBackground(i, Const.RED);
					}
				}
				TableItem temp = new TableItem(table, SWT.NONE);
				temp.setText(0, String.valueOf(table.getItemCount()));
				temp.setText(1, " Hidden Field ");
				temp.setText(2, " No need \"" + fieldToCheck[j] + "\"");
				temp.setText(3, map.getNamedItem(PropertyClass.id)
						.getNodeValue());
				temp.setBackground(Const.RED);
				temp.setData(Const.TABLEITEM, item);
			}
		}

		int widthColNo = 0;
		for (int i = 0; i < item.getParent().getColumnCount(); i++) {
			if (item.getParent().getColumn(i).getText().equals(
					PropertyClass.width)) {
				widthColNo = i;
			}
		}
		Node node2 = map.getNamedItem(PropertyClass.width);
		int currentWidth = node2 == null ? Integer.valueOf(
				item.getText(widthColNo)).intValue() : Integer.valueOf(
				node2.getNodeValue()).intValue();
		int width = 0;
		boolean check = false;
		if ((map.getNamedItem(PropertyClass.id).getNodeValue().indexOf(
				Const.HID_) >= 0)) {
			if (map.getNamedItem(PropertyClass.put).getNodeValue().indexOf(
					Const.LIST) < 0) {
				width = 5;
			}
			check = currentWidth != width;
		}
		if (check) {
			Color color = map.getNamedItem(PropertyClass.readonly) == null ? Const.RED
					: Const.YELLOW;
			item.setBackground(widthColNo, color);
			TableItem temp = new TableItem(table, SWT.NONE);
			temp.setText(0, String.valueOf(table.getItemCount()));
			temp.setText(1, " Wrong width ");
			temp.setText(2, " Need: " + width + " - Actual: " + currentWidth);
			temp.setText(3, map.getNamedItem(PropertyClass.id).getNodeValue());
			temp.setBackground(color);
			temp.setData(Const.TABLEITEM, item);
		} else {
			item.setBackground(Const.DEFAULT_COLOR);
		}
	}

	private static void checkMaxLength(Table table, TableItem item, Node node) {
		if (node.getNodeName() != ComponentClass.text
				&& node.getNodeName() != ComponentClass.label) {
			return;
		}
		int widthColNo = 0;
		for (int i = 0; i < item.getParent().getColumnCount(); i++) {
			if (item.getParent().getColumn(i).getText().equals(
					PropertyClass.width)) {
				widthColNo = i;
			}
		}
		NamedNodeMap map = node.getAttributes();
		try {
			Node node2 = map.getNamedItem(PropertyClass.width);
			int currentWidth = node2 == null ? Integer.valueOf(
					item.getText(widthColNo)).intValue() : Integer.valueOf(
					node2.getNodeValue()).intValue();
			node2 = map.getNamedItem(PropertyClass.maxlen);
			int maxLenth = node2 != null ? Integer
					.valueOf(node2.getNodeValue()).intValue() : 0;
			if (node.getNodeName() == ComponentClass.label) {
				// node2 = map.getNamedItem(PropertyClass.data);
				// String data = node2 != null ? node2.getNodeValue() : "";
				// maxLenth = data.;
				// System.out.print(data + " " + maxLenth + "\n");
			}
			if (maxLenth == 0) {
				return;
			}
			int width = (map.getNamedItem(PropertyClass.put).getNodeValue()
					.indexOf(Const.LIST) < 0) ? Utils.calWidth(maxLenth, 11)
					: Utils.calWidth(maxLenth, 10);
			boolean check = node.getNodeName() != ComponentClass.text ? currentWidth != width
					: currentWidth < width;

			if (check) {
				Color color = map.getNamedItem(PropertyClass.readonly) == null ? Const.RED
						: Const.YELLOW;
				item.setBackground(widthColNo, color);
				TableItem temp = new TableItem(table, SWT.NONE);
				temp.setText(0, String.valueOf(table.getItemCount()));
				temp.setText(1, " Wrong width ");
				temp.setText(2, " Need: " + width + " - Actual: "
						+ currentWidth);
				temp.setText(3, map.getNamedItem(PropertyClass.id)
						.getNodeValue());
				temp.setBackground(color);
				temp.setData(Const.TABLEITEM, item);
			} else {
				item.setBackground(Const.DEFAULT_COLOR);
			}
		} catch (Exception e) {
		}

	}

	private static void checkRelatePosition(Table table, List listItem,
			List listNode) {
		for (int i = 0; i < listItem.size() - 1; i++) {
			NodeModel nodeModel = new NodeModel((Node) listNode.get(i));
			NodeModel nodeModelNext = new NodeModel((Node) listNode.get(i + 1));
			TableItem itemNext = (TableItem) listItem.get(i + 1);
			if (nodeModel.getTop() < 0 || nodeModelNext.getTop() < 0) {
				continue;
			}
			boolean sameGroup = checkSameGroup(nodeModel, nodeModelNext);
			if (sameGroup) {
				int leftTemp = nodeModel.getLeft() + nodeModel.getWidth();
				if (nodeModel.getName().equals(ComponentClass.label)) {
					if (leftTemp != nodeModelNext.getLeft() - 10) {
						String[] message = new String[] {
								" Wrong left position",
								" Need: " + (leftTemp + 10) + " - Actual: "
										+ nodeModelNext.getLeft(),
								nodeModelNext.getId(),
								" relate with " + nodeModel.getId() };
						Utils.logFine(table, itemNext, PropertyClass.left,
								message, Const.WARNING);

					}
				} else {
					if (leftTemp != nodeModelNext.getLeft()) {
						String[] message = new String[] {
								" Wrong left position",
								" Need: " + leftTemp + " - Actual: "
										+ nodeModelNext.getLeft(),
								nodeModelNext.getId(),
								" relate with " + nodeModel.getId() };
						Utils.logFine(table, itemNext, PropertyClass.left,
								message, Const.WARNING);
					}
				}
			}
		}
	}

	private static boolean checkSameGroup(NodeModel nodeModel,
			NodeModel nodeModelNext) {
		int ep = 10;
		int leftTemp = nodeModel.getLeft() + nodeModel.getWidth();
		int topDiff = nodeModel.getTop() - nodeModelNext.getTop();
		if (nodeModel.getName().equals(ComponentClass.label)) {
			ep = 20;
		}
		boolean check = nodeModel.getPut().indexOf(Const.KEY_) < 0
				&& nodeModel.getName().equals(ComponentClass.text);
		String commonId = nodeModel.getId().substring(
				check ? 0 : nodeModel.getId().indexOf("_") + 1);
		// System.out.print(nodeModel.getId()
		// + " - "
		// + nodeModelNext.getId()
		// + " - "
		// + (Math.abs(nodeModelNext.getLeft() - leftTemp) < ep
		// && nodeModelNext.getId().indexOf(commonId) >= 0 && Math
		// .abs(topDiff) < 5) + "\n");
		return Math.abs(nodeModelNext.getLeft() - leftTemp) < ep
				&& nodeModelNext.getId().indexOf(commonId) >= 0
				&& Math.abs(topDiff) < 5;
	}

	private static void checkPosition(Table table, TableItem item, Node node) {
		String[] message = new String[] { "", "", "", "" };
		NodeModel nodeModel = new NodeModel(node);
		if (nodeModel.getId().indexOf(Const.LINE) >= 0
				|| nodeModel.getName().equals(ComponentClass.window)
				|| nodeModel.getPut().indexOf(Const.LIST) >= 0) {
			return;
		}
		boolean isButton = (nodeModel.getName().equals(ComponentClass.button) && node
				.getAttributes().getNamedItem(PropertyClass.type) == null);
		int stdHeight = -1;
		if (nodeModel.getName().equals(ComponentClass.label)
				|| nodeModel.getName().equals(ComponentClass.text) || isButton
				|| nodeModel.getName().equals(ComponentClass.combo)) {
			stdHeight = 20;
		} else if (nodeModel.getName().equals(ComponentClass.group)) {
			stdHeight = 30;
		} else if (nodeModel.getName().equals(ComponentClass.button)) {
			stdHeight = 16;
		}
		if (stdHeight != -1 && nodeModel.getHeight() % stdHeight != 0) {
			int num = nodeModel.getHeight() % stdHeight <= (stdHeight / 2) ? (nodeModel
					.getHeight() / stdHeight)
					: (nodeModel.getHeight() / stdHeight + 1);
			message[0] = " Wrong height value ";
			message[1] = " Need " + (stdHeight * num) + " - Actual: "
					+ nodeModel.getHeight();
			message[2] = nodeModel.getId();
			Utils.logFine(table, item, PropertyClass.height, message,
					Const.WARNING);
		}
		int stdTop = -1;
		if (nodeModel.getName().equals(ComponentClass.text)
				|| nodeModel.getName().equals(ComponentClass.combo)) {
			stdTop = 0;
		} else if (nodeModel.getName().equals(ComponentClass.label)) {
			stdTop = 3;
		} else if (nodeModel.getName().equals(ComponentClass.button)) {
			stdTop = 1;
		}
		// if (stdTop != -1 && nodeModel.getPut().indexOf(GROUP) >= 0) {
		// stdTop += 10;
		// }
		if (stdTop != -1 && nodeModel.getTop() % 10 != stdTop) {
			int num = nodeModel.getTop() % 10 <= (stdTop + 5) ? (nodeModel
					.getTop() / 10) : (nodeModel.getTop() / 10 + 1);
			message[0] = " Wrong top value ";
			message[1] = " Need " + (10 * num + stdTop) + " - Actual: "
					+ nodeModel.getTop();
			message[2] = nodeModel.getId();
			Utils.logFine(table, item, PropertyClass.top, message,
					Const.WARNING);
		}
		int stdLeft = -1;
		if (nodeModel.getName().equals(ComponentClass.text)
				|| nodeModel.getName().equals(ComponentClass.label)
				|| nodeModel.getName().equals(ComponentClass.button)
				|| nodeModel.getName().equals(ComponentClass.combo)
				|| nodeModel.getName().equals(ComponentClass.group)) {
			stdLeft = 5;
		}
		if (stdLeft != -1 && nodeModel.getLeft() % stdLeft != 0) {
			int num = nodeModel.getLeft() % stdLeft <= (stdLeft / 2) ? (nodeModel
					.getLeft() / stdLeft)
					: (nodeModel.getLeft() / stdLeft + 1);
			message[0] = " Wrong left value ";
			message[1] = " Need " + (stdLeft * num) + " - Actual: "
					+ nodeModel.getLeft();
			message[2] = nodeModel.getId();
			Utils.logFine(table, item, PropertyClass.left, message,
					Const.WARNING);
		}
	}

	private static void checkId(Table table, TableItem item, Node node) {
		String[] message = new String[] { "", "", "", "" };
		NodeModel nodeModel = new NodeModel(node);
		if (nodeModel.getId().indexOf(Const.LINE) >= 0
				|| nodeModel.getName().equals(ComponentClass.window)) {
			return;
		}
		if (nodeModel.getName().equals(ComponentClass.label)
				&& nodeModel.getPut().indexOf(Const.LIST) < 0) {
			if (nodeModel.getId().indexOf(Const.LABEL_) < 0
					&& nodeModel.getId().indexOf(Const.HID_) < 0) {
				message[0] = " Wrong id value ";
				message[1] = " Need prefix: " + Const.LABEL_;
				message[2] = nodeModel.getId();
			}
		}
		if (nodeModel.getName().equals(ComponentClass.button)) {
			int index = 0;
			if (node.getAttributes().getNamedItem(PropertyClass.type) != null) {
				index = Integer.parseInt(node.getAttributes().getNamedItem(
						PropertyClass.type).getNodeValue());
			}
			if (index > 0) {
				if (nodeModel.getId().indexOf(Const.KEY_) >= 0) {
					return;
				}
			}
			if (nodeModel.getId().indexOf(Const.prefixButton[index]) < 0) {
				message[0] = " Wrong id value ";
				message[1] = " Need prefix: " + Const.prefixButton[index];
				message[2] = nodeModel.getId();
			}
		}
		if (((nodeModel.getName().equals(ComponentClass.text) || nodeModel
				.getName().equals(ComponentClass.combo)) && nodeModel.getPut()
				.indexOf(Const.LIST) < 0)
				&& nodeModel.getPut().indexOf(Const.KEY_) >= 0) {
			if (nodeModel.getId().indexOf(Const.KEY_) < 0
					&& nodeModel.getId().indexOf(Const.HID_) < 0) {
				message[0] = " Wrong id value ";
				message[1] = " Need prefix: " + Const.KEY_;
				message[2] = nodeModel.getId();
			}
		}
		if (message[0].length() > 0) {
			Utils
					.logFine(table, item, PropertyClass.id, message,
							Const.WARNING);
		}
	}

	private static boolean checkSameArea(NodeModel nodeModel,
			NodeModel nodeModelNext) {
		int ep = 10;
		int leftTemp = nodeModel.getLeft() + nodeModel.getWidth();
		int topDiff = nodeModel.getTop() - nodeModelNext.getTop();
		if (nodeModel.getName().equals(ComponentClass.label)) {
			ep = 20;
		}
		// System.out.print(nodeModel.getId()
		// + " - "
		// + nodeModelNext.getId()
		// + " - "
		// + (Math.abs(nodeModelNext.getLeft() - leftTemp) < ep && Math
		// .abs(topDiff) < 5) + "\n");
		return Math.abs(nodeModelNext.getLeft() - leftTemp) < ep
				&& Math.abs(topDiff) < 5;
	}

	private static void checkRelateId(Table table, List listItem, List listNode) {
		for (int i = 0; i < listItem.size() - 1; i++) {
			NodeModel nodeModel = new NodeModel((Node) listNode.get(i));
			NodeModel nodeModelNext = new NodeModel((Node) listNode.get(i + 1));
			TableItem itemNext = (TableItem) listItem.get(i + 1);
			if (nodeModel.getId().indexOf(Const.LINE) >= 0
					|| nodeModel.getName().equals(ComponentClass.window)
					|| nodeModel.getPut().indexOf(Const.LIST) >= 0
					|| nodeModel.getId().indexOf(Const.MENU) >= 0) {
				continue;
			}
			if (nodeModel.getId().indexOf(Const.HID_) >= 0
					&& nodeModelNext.getId().indexOf(Const.HID_) >= 0) {
				continue;
			}
			if (nodeModel.getTop() < 0 || nodeModelNext.getTop() < 0) {
				continue;
			}
			boolean sameArea = checkSameArea(nodeModel, nodeModelNext);
			// System.out.print(sameArea + "\n");
			if (sameArea) {
				String id1 = nodeModel.getId();
				String id2 = nodeModelNext.getId();
				for (int j = 0; j < Const.commonToCheckId.length; j++) {
					id1 = id1.replace(Const.commonToCheckId[j], "");
					id2 = id2.replace(Const.commonToCheckId[j], "");
				}
				if (!id1.equals(id2)) {
					String[] message = new String[] {
							" Wrong id value",
							" Need: " + nodeModelNext.getId().replace(id2, id1),
							nodeModelNext.getId(),
							" relate with " + nodeModel.getId() };
					Utils.logFine(table, itemNext, PropertyClass.id, message,
							Const.WARNING);
				}
			}
		}
	}

	public static void checkWitdh(TableEditor editor, int EDITABLECOLUMN) {
		String text = !editor.getItem().isDisposed() ? editor.getItem()
				.getText(EDITABLECOLUMN) : "";
		try {
			int temp = Integer.valueOf(text).intValue();
			if (temp < 0 && !editor.getItem().isDisposed()) {
				editor.getItem().setText(EDITABLECOLUMN, "");
				editor.getItem().setText(EDITABLECOLUMN + 1, "");
				editor.getItem().setText(EDITABLECOLUMN + 2, "");
			}
			int suggestFont11 = 0;
			int suggestFont10 = 0;
			suggestFont11 = Utils.calWidth(temp, 11);
			suggestFont10 = Utils.calWidth(temp, 10);
			editor.getItem().setText(EDITABLECOLUMN + 1, suggestFont11 + "");
			editor.getItem().setText(EDITABLECOLUMN + 2, "" + suggestFont10);
			editor.getItem().setBackground(EDITABLECOLUMN + 1, Const.GREEN);
			editor.getItem().setBackground(EDITABLECOLUMN + 2, Const.GREEN);
			int currentWidth = Integer.valueOf(
					editor.getItem().getText(EDITABLECOLUMN - 1)).intValue();
			if (editor.getItem().getText(1).indexOf(Const.LIST) < 0) {
				if (currentWidth != suggestFont11) {
					editor.getItem().setBackground(EDITABLECOLUMN - 1,
							Const.RED);
				} else {
					editor.getItem().setBackground(EDITABLECOLUMN - 1,
							Const.DEFAULT_COLOR);
				}
			}
			if (editor.getItem().getText(1).indexOf(Const.LIST) >= 0) {
				if (currentWidth != suggestFont10) {
					editor.getItem().setBackground(EDITABLECOLUMN - 1,
							Const.RED);
				} else {
					editor.getItem().setBackground(EDITABLECOLUMN - 1,
							Const.DEFAULT_COLOR);
				}
			}
		} catch (Exception e) {
			if (!editor.getItem().isDisposed()) {
				editor.getItem().setText(EDITABLECOLUMN, "");
				editor.getItem().setText(EDITABLECOLUMN + 1, "");
				editor.getItem().setText(EDITABLECOLUMN + 2, "");
			}
		}
	}

	public static void checkItemRulesForText(Table table, TableItem item,
			NodeModel nodeModel, ItemRules itemRulesModel, boolean isLog) {
		itemRulesModel.getMaxLength();
		if (nodeModel.getMaxlen() != itemRulesModel.getMaxLength()) {
			String[] message = new String[] {
					" Wrong maxlen value",
					" Need: " + itemRulesModel.getMaxLength() + " - Actual: "
							+ nodeModel.getMaxlen(), nodeModel.getId(),
					itemRulesModel.getFieldName() };
			if (nodeModel.getMaxlen() == -1) {
				message[1] = " Need " + itemRulesModel.getMaxLength()
						+ " - Actual: Not input";
			}
			if (itemRulesModel.getMaxLength() == -1) {
				message[1] = " No need maxlen";
			}
			Utils.setCellColor(item, PropertyClass.maxlen, Const.RED);
			if (isLog) {
				Utils.logFine(table, item, PropertyClass.maxlen, message,
						Const.ERROR);
			}
		} else {
			Utils.setCellColor(item, PropertyClass.maxlen, Const.DEFAULT_COLOR);
		}
		if (nodeModel.getMinlen() != itemRulesModel.getMinLength()) {
			String[] message = new String[] {
					" Wrong minlen value",
					" Need: " + itemRulesModel.getMinLength() + " - Actual: "
							+ nodeModel.getMinlen(), nodeModel.getId(),
					itemRulesModel.getFieldName() };
			if (nodeModel.getMinlen() == 0) {
				message[1] = " Need " + itemRulesModel.getMinLength()
						+ " - Actual: Not input";
			}
			if (itemRulesModel.getMinLength() == 0) {
				message[1] = " No need minlen";
			}
			Utils.setCellColor(item, PropertyClass.minlen, Const.RED);
			if (isLog) {
				Utils.logFine(table, item, PropertyClass.minlen, message,
						Const.ERROR);
			}
		} else {
			Utils.setCellColor(item, PropertyClass.minlen, Const.DEFAULT_COLOR);
		}
		if (nodeModel.getKind() != itemRulesModel.getKind()) {
			String[] message = new String[] {
					" Wrong kind value",
					" Need: " + itemRulesModel.getKind() + " - Actual: "
							+ nodeModel.getKind(), nodeModel.getId(),
					itemRulesModel.getFieldName() };
			if (itemRulesModel.getKind() == 0) {
				message[1] = " No need kind";
			}
			if (isLog) {
				Utils.logFine(table, item, PropertyClass.kind, message,
						Const.ERROR);
			}
			Utils.setCellColor(item, PropertyClass.kind, Const.RED);
		} else {
			Utils.setCellColor(item, PropertyClass.kind, Const.DEFAULT_COLOR);
		}
		if (nodeModel.getCapitaltype() != itemRulesModel.getCapitalType()) {
			String[] message = new String[] {
					" Wrong capitaltype value",
					" Need: " + itemRulesModel.getCapitalType() + " - Actual: "
							+ nodeModel.getCapitaltype(), nodeModel.getId(),
					itemRulesModel.getFieldName() };
			if (itemRulesModel.getCapitalType() == 0) {
				message[1] = " No need capitaltype";
			}
			Utils.setCellColor(item, PropertyClass.capitaltype, Const.RED);
			if (isLog) {
				Utils.logFine(table, item, PropertyClass.capitaltype, message,
						Const.ERROR);
			}
		} else {
			Utils.setCellColor(item, PropertyClass.capitaltype,
					Const.DEFAULT_COLOR);
		}
	}

	public static void checkItemRulesForLabel(Table table, TableItem item,
			NodeModel nodeModel, ItemRules itemRulesModel, boolean isLog) {
		String proper = PropertyClass.data;
		if (nodeModel.getPut().indexOf(Const.LIST) >= 0) {
			proper = PropertyClass.header;
		}
		for (int j = 0; j < item.getParent().getColumnCount(); j++) {
			String property = item.getParent().getColumn(j).getText();
			if (property.equals(proper)) {
				nodeModel.setLabel(item.getText(j));
			}
		}

		if (!nodeModel.getLabel().equals(itemRulesModel.getLabel())
				&& nodeModel.getId().indexOf(Const.HID_) < 0
				&& itemRulesModel.getLabel().length() > 0) {
			String[] message = new String[] {
					" Wrong data value",
					" Need: " + itemRulesModel.getLabel() + " - Actual: "
							+ nodeModel.getLabel(), nodeModel.getId(),
					itemRulesModel.getFieldName() };

			Utils.setCellColor(item, proper, Const.RED);
			if (isLog) {
				Utils.logFine(table, item, proper, message, Const.ERROR);
			}
		} else {
			Utils.setCellColor(item, PropertyClass.data, Const.DEFAULT_COLOR);
		}
	}

	public static void checkItemRulesList(Table table, TableItem item,
			Node node, List itemRules) {
		Iterator ite = itemRules.iterator();
		while (ite.hasNext()) {
			ItemRules tmp = (ItemRules) ite.next();
			NodeModel nodeModel = new NodeModel(node);
			String id1 = nodeModel.getId();
			if (id1.indexOf(Const.HID_) >= 0
					|| node.getAttributes()
							.getNamedItem(PropertyClass.readonly) != null) {
				continue;
			}
			for (int j = 0; j < Const.commonToCheckItemRule.length; j++) {
				id1 = id1.replace(Const.commonToCheckItemRule[j], "");
			}
			String id2 = tmp.getFieldName().split(" ")[0].toUpperCase();
			if (id1.equals(id2)) {
				for (int j = 0; j < item.getParent().getColumnCount(); j++) {
					String property = item.getParent().getColumn(j).getText();
					if (property.equals(Const.ITEM_RULES)) {
						item.setText(j, tmp.getFieldName());
						item.setText(j + 1, tmp.getLabel());
					}
				}
				if (node.getNodeName().equals(ComponentClass.text)) {
					checkItemRulesForText(table, item, nodeModel, tmp, true);
				} else {
					checkItemRulesForLabel(table, item, nodeModel, tmp, true);
				}
			}
		}
	}
}
